home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-06-23 | 10.6 KB | 397 lines |
- import java.awt.*;
- import java.awt.event.KeyEvent;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
- /**
- * Apple Worldwide Developer Technical Support
- *
- * Sample showing how to send and receive AppleEvents using JDirect 2.
- *
- * File: AESendFrame.java
- *
- * This class contains this sample's user interface for sending AppleEvents.
- *
- * @author Levi Brown
- * @author Apple Computer, Inc.
- *
- * Copyright ©1999 Apple Computer, Inc.
- * All rights reserved.
- *
- * @version 1.0
- * 4/15/1999 Shipped as 'AppleEvent Send and Receive' sample.
- *
- * You may incorporate this sample code into your applications without
- * restriction, though the sample code has been provided "AS IS" and the
- * responsibility for its operation is 100% yours. However, what you are
- * not permitted to do is to redistribute the source as "Apple Sample
- * Code" after having made changes. If you're going to re-distribute the
- * source, we require that you make it clear in the source that the code
- * was descended from Apple Sample Code, but that you've made changes.
- */
- public class AESendFrame extends Frame
- {
- /**
- * Creates the UI for the AESend example.
- */
- public AESendFrame()
- {
- //{{INIT_CONTROLS
- GridBagLayout gridBagLayout;
- gridBagLayout = new GridBagLayout();
- setLayout(gridBagLayout);
- setVisible(false);
- setSize(466,90);
- setBackground(new Color(-1052689));
- divider1 = new Divider();
- divider1.setBounds(5,51,456,2);
- GridBagConstraints gbc;
- gbc = new GridBagConstraints();
- gbc.gridx = 1;
- gbc.gridy = 4;
- gbc.gridwidth = 2;
- gbc.weightx = 0.5;
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.insets = new Insets(0,5,0,5);
- ((GridBagLayout)getLayout()).setConstraints(divider1, gbc);
- add(divider1);
- label1 = new java.awt.Label("Message to Send:");
- label1.setBounds(5,4,94,21);
- label1.setFont(new Font("SansSerif", Font.BOLD, 9));
- gbc = new GridBagConstraints();
- gbc.gridx = 1;
- gbc.gridy = 1;
- gbc.anchor = GridBagConstraints.WEST;
- gbc.fill = GridBagConstraints.NONE;
- gbc.insets = new Insets(0,5,0,0);
- ((GridBagLayout)getLayout()).setConstraints(label1, gbc);
- add(label1);
- sendButton = new java.awt.Button();
- sendButton.setLabel("Send Event");
- sendButton.setBounds(376,25,85,20);
- gbc = new GridBagConstraints();
- gbc.gridx = 2;
- gbc.gridy = 2;
- gbc.fill = GridBagConstraints.NONE;
- gbc.insets = new Insets(0,0,0,5);
- ((GridBagLayout)getLayout()).setConstraints(sendButton, gbc);
- add(sendButton);
- textField1 = new java.awt.TextField();
- textField1.setText("Hello there, other Java applicaion!");
- textField1.setBounds(10,25,361,21);
- textField1.setFont(new Font("SansSerif", Font.PLAIN, 9));
- textField1.setBackground(new Color(16777215));
- gbc = new GridBagConstraints();
- gbc.gridx = 1;
- gbc.gridy = 2;
- gbc.weightx = 0.5;
- gbc.anchor = GridBagConstraints.WEST;
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.insets = new Insets(0,10,5,5);
- ((GridBagLayout)getLayout()).setConstraints(textField1, gbc);
- add(textField1);
- panel1 = new java.awt.Panel();
- gridBagLayout = new GridBagLayout();
- panel1.setLayout(gridBagLayout);
- panel1.setBounds(0,53,466,21);
- gbc = new GridBagConstraints();
- gbc.gridx = 1;
- gbc.gridy = 5;
- gbc.gridwidth = 2;
- gbc.weightx = 0.5;
- gbc.anchor = GridBagConstraints.WEST;
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.insets = new Insets(0,0,0,0);
- ((GridBagLayout)getLayout()).setConstraints(panel1, gbc);
- add(panel1);
- statusLabel = new java.awt.Label("Idle");
- statusLabel.setBounds(50,0,416,21);
- statusLabel.setFont(new Font("SansSerif", Font.PLAIN, 9));
- gbc = new GridBagConstraints();
- gbc.gridx = 2;
- gbc.gridy = 1;
- gbc.weightx = 0.5;
- gbc.anchor = GridBagConstraints.WEST;
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.insets = new Insets(0,0,0,0);
- ((GridBagLayout)panel1.getLayout()).setConstraints(statusLabel, gbc);
- panel1.add(statusLabel);
- label2 = new java.awt.Label("Status:");
- label2.setBounds(5,0,40,21);
- label2.setFont(new Font("SansSerif", Font.BOLD, 9));
- gbc = new GridBagConstraints();
- gbc.gridx = 1;
- gbc.gridy = 1;
- gbc.anchor = GridBagConstraints.WEST;
- gbc.fill = GridBagConstraints.NONE;
- gbc.insets = new Insets(0,5,0,5);
- ((GridBagLayout)panel1.getLayout()).setConstraints(label2, gbc);
- panel1.add(label2);
- panel2 = new java.awt.Panel();
- gridBagLayout = new GridBagLayout();
- panel2.setLayout(gridBagLayout);
- panel2.setBounds(0,53,466,21);
- gbc = new GridBagConstraints();
- gbc.gridx = 1;
- gbc.gridy = 3;
- gbc.gridwidth = 2;
- gbc.weightx = 0.5;
- gbc.anchor = GridBagConstraints.WEST;
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.insets = new Insets(0,0,0,0);
- ((GridBagLayout)getLayout()).setConstraints(panel2, gbc);
- add(panel2);
- replyLabel = new java.awt.Label("Idle");
- replyLabel.setBounds(50,0,416,21);
- replyLabel.setFont(new Font("SansSerif", Font.PLAIN, 9));
- gbc = new GridBagConstraints();
- gbc.gridx = 2;
- gbc.gridy = 1;
- gbc.weightx = 0.5;
- gbc.anchor = GridBagConstraints.WEST;
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.insets = new Insets(0,0,0,0);
- ((GridBagLayout)panel2.getLayout()).setConstraints(replyLabel, gbc);
- panel2.add(replyLabel);
- label4 = new java.awt.Label("Reply:");
- label4.setBounds(5,0,40,21);
- label4.setFont(new Font("SansSerif", Font.BOLD, 9));
- gbc = new GridBagConstraints();
- gbc.gridx = 1;
- gbc.gridy = 1;
- gbc.anchor = GridBagConstraints.WEST;
- gbc.fill = GridBagConstraints.NONE;
- gbc.insets = new Insets(0,5,0,5);
- ((GridBagLayout)panel2.getLayout()).setConstraints(label4, gbc);
- panel2.add(label4);
- setTitle("AESendTest");
- //}}
-
- //{{INIT_MENUS
- //}}
-
- //{{REGISTER_LISTENERS
- SymWindow aSymWindow = new SymWindow();
- this.addWindowListener(aSymWindow);
- SymAction lSymAction = new SymAction();
- sendButton.addActionListener(lSymAction);
- textField1.addActionListener(lSymAction);
- //}}
-
- //Create a new timer to handle status messages.
- statusTimer = new Timer(kEraseSleep, new TimerCallback()
- {
- public void timeIsUp()
- {
- isEraseStatus = false;
- try
- {
- setStatus("Idle");
- }
- finally
- {
- isEraseStatus = true;
- }
- }
- });
-
- isEraseStatus = true;
-
- //Create a new timer to handle reply messages.
- replyTimer = new Timer(kEraseSleep, new TimerCallback()
- {
- public void timeIsUp()
- {
- isEraseReply = false;
- try
- {
- setReply("Idle");
- }
- finally
- {
- isEraseReply = true;
- }
- }
- });
-
- isEraseReply = true;
-
- //Create a new AESend object so we can send our event later.
- aeSend = new AESend();
- //Register a new instance of our ActionListener with the AEReceive object
- //so we get notified when an AppleEvent is received.
- aeSend.addActionListener(new Action());
- }
-
- /**
- * Shows or hides the component depending on the boolean flag b.
- * @param b if true, show the component; otherwise, hide the component.
- * @see java.awt.Component#isVisible
- */
- public void setVisible(boolean b)
- {
- if(b)
- {
- setLocation(50, 50);
- }
- super.setVisible(b);
- }
-
- /**
- * The entry point to our application
- */
- static public void main(String args[])
- {
- (new AESendFrame()).setVisible(true);
- }
-
- //{{DECLARE_CONTROLS
- Divider divider1;
- java.awt.Label label1;
- java.awt.Button sendButton;
- java.awt.TextField textField1;
- java.awt.Panel panel1;
- java.awt.Label statusLabel;
- java.awt.Label label2;
- java.awt.Panel panel2;
- java.awt.Label replyLabel;
- java.awt.Label label4;
- //}}
-
- //{{DECLARE_MENUS
- //}}
-
- /**
- * Set the text of the statusLabel
- * @param the message to use.
- */
- protected void setStatus(String message)
- {
- statusLabel.setText(message);
- if (isEraseStatus)
- statusTimer.start();
- }
-
- /**
- * Set the text of the message replyLabel
- * @param the message to use.
- */
- protected void setReply(String message)
- {
- replyLabel.setText(message);
- if (isEraseReply)
- replyTimer.start();
- }
-
- /**
- * A function to simulate a click on the target button.
- * This will make the button draw as if it had been pressed and
- * released, and the button will fire an Action event as if
- * the button were pressed.
- * For use with the Apple MRJ 2.1 EA3 and later.
- */
- protected void simulateClick(Button target)
- {
- if (target != null)
- {
- KeyEvent keyEvent = new KeyEvent(target, KeyEvent.KEY_PRESSED,
- System.currentTimeMillis(), 0, KeyEvent.VK_ENTER,
- (char)KeyEvent.VK_ENTER);
- target.dispatchEvent(keyEvent);
- }
- }
-
- /**
- * An inner class designed to listen for ActionEvents
- * from the AESend class.
- */
- class Action implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- setReply(event.getActionCommand());
- }
- }
-
- class SymWindow extends java.awt.event.WindowAdapter
- {
- public void windowClosing(java.awt.event.WindowEvent event)
- {
- Object object = event.getSource();
- if (object == AESendFrame.this)
- AESendFrame_WindowClosing(event);
- }
- }
-
- void AESendFrame_WindowClosing(java.awt.event.WindowEvent event)
- {
- setVisible(false); // hide the AESendFrame
- dispose(); // free the system resources
- System.exit(0); // close the application
- }
-
- class SymAction implements java.awt.event.ActionListener
- {
- public void actionPerformed(java.awt.event.ActionEvent event)
- {
- Object object = event.getSource();
- if (object == sendButton)
- sendButton_ActionPerformed(event);
- else if (object == textField1)
- textField1_EnterHit(event);
-
- }
- }
-
- void sendButton_ActionPerformed(java.awt.event.ActionEvent event)
- {
- String text = textField1.getText();
- if (text != null && !text.equals(""))
- {
- setStatus("Sending AppleEvent...");
- try
- {
- aeSend.sendAppleEvent(text);
- }
- catch (NativeException exc)
- {
- Toolkit.getDefaultToolkit().beep();
- setStatus(exc.getMessage() + " (#" + exc.getErrNum() + ")");
- return;
- }
- setStatus("AppleEvent Sent");
- }
- }
-
- void textField1_EnterHit(java.awt.event.ActionEvent event)
- {
- simulateClick(sendButton);
- }
-
- /**
- * The time in milliseconds to wait before removing the previous status message.
- */
- protected static final int kEraseSleep = 1250;
- /**
- * Our AESend object which allows us to send the AppleEvent
- */
- protected AESend aeSend;
- /**
- * An internal flag to prevent recursion in the status message handling code.
- */
- protected boolean isEraseStatus;
- /**
- * An internal flag to prevent recursion in the reply message handling code.
- */
- protected boolean isEraseReply;
- /**
- * A timer to keep track of displaying the status message.
- */
- protected Timer statusTimer;
- /**
- * A timer to keep track of displaying the reply message.
- */
- protected Timer replyTimer;
- }
-
-